{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch.utils.data import Dataset\n",
    "\n",
    "\n",
    "class MyDataset(Dataset):\n",
    "    # 构造函数，自定义数据读取方法以及对数据进行预处理\n",
    "    def __init__(self, data_tensor, target_tensor):\n",
    "        self.data_tensor = data_tensor\n",
    "        self.target_tensor = target_tensor\n",
    "    # 返回数据集大小\n",
    "\n",
    "    def __len__(self):\n",
    "        return self.data_tensor.size(0)\n",
    "    # 返回索引对应的的数据与标签\n",
    "\n",
    "    def __getitem__(self, index):\n",
    "        return self.data_tensor[index], self.target_tensor[index]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-1.6962, -1.1627, -1.0416],\n",
      "        [ 1.6792, -0.7070, -1.7534],\n",
      "        [ 0.7228, -1.8589, -0.7446],\n",
      "        [ 0.1671,  0.0726,  1.2262],\n",
      "        [-0.8426, -0.5759, -0.4959],\n",
      "        [ 1.8304, -0.8030,  0.1914],\n",
      "        [ 0.7673,  0.1852, -0.5246],\n",
      "        [-0.7971,  0.8786,  1.1336],\n",
      "        [ 0.5264,  0.0815, -1.7966],\n",
      "        [-0.3232, -0.4581,  0.5267]])\n"
     ]
    }
   ],
   "source": [
    "# 随机生成 10 * 3 维度的数据\n",
    "data_tensor = torch.randn(10, 3)\n",
    "print(data_tensor)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([0, 1, 1, 0, 0, 0, 0, 0, 1, 1])\n"
     ]
    }
   ],
   "source": [
    "# 随机生成标签是 0 或 1\n",
    "target_tensor = torch.randint(2, (10, ))\n",
    "print(target_tensor)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dataset size:  10\n",
      "tensor_data[0]:  (tensor([-1.6962, -1.1627, -1.0416]), tensor(0))\n"
     ]
    }
   ],
   "source": [
    "# 将数据封装成 MyDataset\n",
    "my_dataset = MyDataset(data_tensor, target_tensor)\n",
    "\n",
    "# 查看数据集大小\n",
    "print('Dataset size: ', len(my_dataset))\n",
    "# 使用索引获取数据\n",
    "print('tensor_data[0]: ', my_dataset[0])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DateLoader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from torch.utils.data import DataLoader\n",
    "\n",
    "tensor_dataloader = DataLoader(\n",
    "    dataset=my_dataset,     # 传入的数据集\n",
    "    batch_size=3,           # 每个 batch 有多少个样本\n",
    "    shuffle=True,           # 在每个 epoch 开始的时候，是否对数据进行重新打乱\n",
    "    num_workers=0           # 加载数据进程数，0为主进程\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-0.7971,  0.8786,  1.1336],\n",
      "        [ 0.7228, -1.8589, -0.7446],\n",
      "        [ 1.6792, -0.7070, -1.7534]]) tensor([0, 1, 1])\n",
      "tensor([[ 0.1671,  0.0726,  1.2262],\n",
      "        [-0.8426, -0.5759, -0.4959],\n",
      "        [-1.6962, -1.1627, -1.0416]]) tensor([0, 0, 0])\n",
      "tensor([[ 0.7673,  0.1852, -0.5246],\n",
      "        [ 0.5264,  0.0815, -1.7966],\n",
      "        [-0.3232, -0.4581,  0.5267]]) tensor([0, 1, 1])\n",
      "tensor([[ 1.8304, -0.8030,  0.1914]]) tensor([0])\n"
     ]
    }
   ],
   "source": [
    "# 循环输出\n",
    "for data, target in tensor_dataloader:\n",
    "    print(data, target)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "One batch tensor data: [tensor([[ 0.5264,  0.0815, -1.7966],\n",
      "        [-1.6962, -1.1627, -1.0416],\n",
      "        [ 0.7673,  0.1852, -0.5246]]), tensor([1, 0, 0])]\n"
     ]
    }
   ],
   "source": [
    "# 输出一个 batch\n",
    "print('One batch tensor data:', next(iter(tensor_dataloader)))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.10 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "c2561295c99b9e0691d04acbb6873b4734b14bb29878b65a5e35075f8e76500e"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
